package com.andrew.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.dao.BookDao;
import com.andrew.dao.CommentDao;
import com.andrew.dao.impl.BookDaoImpl;
import com.andrew.dao.impl.CommentDaoImpl;
import com.andrew.dao.impl.OrderDaoImpl;
import com.andrew.entity.Book;
import com.andrew.entity.Comment;
import com.andrew.entity.Customer;
import com.andrew.entity.Order;
import com.andrew.entity.OrderDetail;
import com.andrew.service.CommentService;

public class CommentServiceImpl implements CommentService{
	private CommentDao commentDao=new CommentDaoImpl();
	private BookDao bookDao=new BookDaoImpl();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 写评论
	 */
	@Override
	public void writeComment(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		//获取订单详情编号
		int orderDeatilId=Integer.parseInt(req.getParameter("orderDeatilId"));
		//获取订单号
		String orderId=req.getParameter("orderId");
		//获取书本id
		int bookId=Integer.parseInt(req.getParameter("bookId"));
		//获取书本详情
		Book book = bookDao.queryBookbyBookId(bookId);
		//获取当前用户
		Customer customer=(Customer)req.getSession().getAttribute("customer");
		//获取评论内容
		String commentContent=req.getParameter("commentContent");
		
		//新建一个评论对象
		Comment comment=new Comment();
		//设置评论内容
		comment.setCommentContent(commentContent);
		//设置评论用户
		comment.setCustomer(customer);
		//设置评论所对应的书
		comment.setBook(book);
		//设置评论id	(自动生成)
		comment.setCommentId(0);
		//设置评论时间
		comment.setCommentTime(sdf.format(new Date()));
		//添加评论
		int insertComment = commentDao.InsertComment(comment);
		
		//更新当前订单详情的状态
		updateCurrentOrderDetailStatus(orderDeatilId);
		
		//判断是否当前订单的订单详情全部以及评论
		boolean flag= checkOrderDetailStatus(orderId);
		if (flag) {
			//全部评论之后将订单状态改为3
			new OrderDaoImpl().updateOrderStatus(orderId, 3);
		}
		
		
	}
	
	//以评论修改状态
	private void updateCurrentOrderDetailStatus(int orderDetailID) throws SQLException {
		new OrderDaoImpl().updateOrderDetailStatus(orderDetailID);	
	}
	//确认是否以及全部评论完毕
	private boolean checkOrderDetailStatus(String orderId) throws SQLException{
		 List<OrderDetail> orderDetailsList = new OrderDaoImpl().getOrdersByOrderId(orderId).getOrderDetailsList();
		 for(OrderDetail orderDetail:orderDetailsList) {
			 if(orderDetail.getSTATUS()==0) {
				 return false;
			 }
		 }
		 return true;
	}

	/**
	 * 删除评论
	 */
	@Override
	public void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
		int commentId=Integer.parseInt(req.getParameter("commentId"));
		int delete = commentDao.deleteCommentByCommentId(commentId);
		if(delete>0) {
			resp.getWriter().write("DeleteCommentSuccess");
		}else {
			resp.getWriter().write("DeleteCommentFail");
		}
		
	}

	/**
	 * 获取某本书的所有评论
	 */
	@Override
	public List<Comment> getCommentByBookId(int bookId) throws SQLException {
		return commentDao.queryAllCommentByBookId(bookId);
	}

	/**
	 * 获取全部评论
	 */
	@Override
	public List<Comment> getAllComments() throws SQLException {
		return commentDao.queryAllComments();
	}
	

}
